package com.ys.controller.web.hr;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.hr.ThrOperateRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MyConfig;
import com.ys.controller.api.hr.ApiInterThrOperateRegistrationController;
import com.ys.entity.hr.ThrOperateRegistration;
import com.ys.entity.hr.ThrOperateRegistrationItem;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/hr/thrOperateRegistration")
public class WebThrOperateRegistrationController {
	public static Long MENU_ID = 10201L;

	
	@Autowired
	private CommonApiService<ThrOperateRegistration> commonApiService;

	@Autowired
	private ThrOperateRegistrationService thrOperateRegistrationService;
	/*
	 * 新增
	 */

	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception{
		String bid = commonApiService.getCommonNextBid(ApiInterThrOperateRegistrationController.MENU_ID);
		model.addAttribute("onProcess", 0);
		String aa = commonApiService.toAdd(model, new ThrOperateRegistration().setBid(bid), ApiInterThrOperateRegistrationController.MENU_ID, null, null);
        
        if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
			return "hr/thrOperateRegistration/thrOperateRegistrationAdd";
		} else {
			return aa;
		}
	}
	/*
	 * 编辑
	 */
//    @RequiresPermissions("thrOperateRegistration:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,String bid){
		try {
			ThrOperateRegistration entity = new ThrOperateRegistration().setBid(bid).queryFirst();
			if (entity == null){
				return W.error(model, "已删除!");
			}
			commonApiService.toEdit(model,entity.getId(), new ThrOperateRegistration(), MENU_ID, null, null);
			List<Map<String, Object>> itemList = thrOperateRegistrationService.getItemList(bid);

			YsProcInst yspProcInst = new YsProcInst().setBid(entity.getBid()).queryFirst();
			Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
			model.addAttribute("onProcess", onProcess);
			DetailList detailList = new DetailList(itemList,entity.getId());
			model.addAttribute("detail", detailList);
			return "hr/thrOperateRegistration/thrOperateRegistrationEdit";
		} catch (Exception e) {
			//logger.error("编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

	@Data
	@AllArgsConstructor
	private static class DetailList{
		private List<Map<String, Object>> itemList;
		private Long id;
	}

}
